package com.kc.business.service.impl;

import com.kc.business.domain.BizGoods;
import com.kc.business.domain.BizPlatformConsignmentGoods;
import com.kc.business.mapper.BizPlatformConsignmentGoodsMapper;
import com.kc.business.service.IBizGoodsService;
import com.kc.business.service.IBizPlatformConsignmentGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * TODO
 *
 * @author zhangshuo
 * @version 1.0
 * @date 2020/10/21
 */
@Service
@Slf4j
public class BizPlatformConsignmentGoodsServiceImpl implements IBizPlatformConsignmentGoodsService {
    @Resource
    BizPlatformConsignmentGoodsMapper platformConsignmentGoodsMapper;
    @Resource
    private IBizGoodsService goodsService;

    /**
     * 查询商品列表
     *
     * @param goods
     * @return
     */
    @Override
    public List<BizPlatformConsignmentGoods> getList(BizPlatformConsignmentGoods goods) {
        return platformConsignmentGoodsMapper.select(goods);
    }

    /**
     * 获取商品详情
     * @param goods
     * @return
     */
    @Override
    public BizPlatformConsignmentGoods getInfo(BizPlatformConsignmentGoods goods) {
        return platformConsignmentGoodsMapper.selectByPrimaryKey(goods);
    }

    /**
     * 新增商品
     *
     * @param goods
     * @return
     */
    @Override
    public int add(BizPlatformConsignmentGoods goods) {
        BizGoods bizGoods = this.getGoodsInfo(goods);
        bizGoods.setSkuSpecs(null);
        Integer goodsId = goodsService.add(bizGoods);
        goods.setGoodsId(goodsId);
        goods.setId(goodsId);
        goods.setIsRecommend(goods.getIsRecommend());
        return platformConsignmentGoodsMapper.insertSelective(goods);
    }

    /**
     * 修改商品
     *
     * @param goods
     * @return
     */
    @Override
    public int edit(BizPlatformConsignmentGoods goods) {
        BizPlatformConsignmentGoods platformConsignmentGoods = this.getInfo(goods);
        BizGoods bizGoods = this.getGoodsInfo(goods);
        if(goods.getIsDeleted() == 1){
            bizGoods.setIsDeleted(1);
        }
        bizGoods.setSkuSpecs(null);
        goodsService.edit(bizGoods);
        bizGoods.setStatus(goods.getStatus());
        return platformConsignmentGoodsMapper.updateByPrimaryKeySelective(goods);
    }

    /**
     * 获取商品信息
     * @param goods
     * @return
     */
    public BizGoods getGoodsInfo(BizPlatformConsignmentGoods goods){
        BizGoods bizGoods = new BizGoods(goods.getTitle(),goods.getThumb(),goods.getPrice(),goods.getImgWidth(),goods.getImgHeight(),goods.getStatus(),
                4,new BigDecimal(goods.getShippingFee()),goods.getBannerList(),goods.getVideoUrl(),goods.getVideoImg(),goods.getSort(),goods.getBannerListAttr());
        if(goods.getGoodsId() != null){
            bizGoods.setId(goods.getGoodsId());
        }
        bizGoods.setGoodsType(1);
        bizGoods.setIsForty(0);
        bizGoods.setActivityPrice(new BigDecimal(0));
        bizGoods.setContentImg("");
        bizGoods.setGoodsAddress("");
        bizGoods.setCoupon(0.0);
        bizGoods.setGoodsAddress(goods.getGoodsAddress());
        bizGoods.setCouponEndTime("0000-00-00 00:00:00");
        bizGoods.setTotal(goods.getStock());
        bizGoods.setMarketprice(goods.getPrice());
        bizGoods.setTitle(goods.getTitle());
        return bizGoods;
    }
}
